package com.cxh.filter;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.filter.OncePerRequestFilter;

import com.alibaba.fastjson.JSON;
import com.google.gson.GsonBuilder;
import com.cxh.util.AiuiUnit;
import com.cxh.util.CommonUtility;
import com.cxh.util.HttpUtil;
import com.cxh.util.LogUtil;

public class SemantemeFilter extends OncePerRequestFilter{
	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		final String question = request.getParameter("answer");
		// 第一步先判断机器人Id是否被注册以及问题是否为空
		String code=request.getParameter("code");
		StringBuffer requestURL = request.getRequestURL();
		String queryString = request.getQueryString(); 
		LogUtil.info(requestURL+URLDecoder.decode(queryString,"utf-8"));
		/*if(!this.checkRootCode(code) || StringUtils.isBlank(question)){
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("scene", 0);
			map.put("skill",0);
			map.put("msgType",0);
			map.put("flag", 1);
			map.put("msg", "机器人未注册或者参数不合法！");
			map.put("speak","");
			map.put("url","");
			map.put("set", new Object());
			map.put("skillList",new Object[]{});
			GsonBuilder gb= new GsonBuilder();
			gb.disableHtmlEscaping();
			String json = gb.create().toJson(map);
			System.out.println(json);
			//返回参数
			ajaxOutput(response, JSON.toJSONString(map));
			return;
		}*/
		// 调用AIUI后台
		Map<String, Object> aiuiMap = new HashMap<String, Object>();
		try {
			aiuiMap = AiuiUnit.loadAiuiApi(question, code);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// AIUI返回了结果
		if(!CommonUtility.isEmpty(aiuiMap)){
			// 有需要调用的函数名
			if(!CommonUtility.isEmpty(aiuiMap.get("function"))){
				
				// 所需参数
				request.setAttribute("params", aiuiMap.get("params"));
				request.setAttribute("code", code);
				request.setAttribute("page", aiuiMap.get("page"));
				// 转发到相应技能处理接口
				request.getRequestDispatcher("/controller/AiuiAPI/"+aiuiMap.get("function")).forward(request, response);
			
			}else{
				HashMap<String, Object> requstParam = new HashMap<String,Object>();
				requstParam.put("beginPrice", "");
				requstParam.put("endPrice", "");
				requstParam.put("gear", "");
				requstParam.put("level", "");
				requstParam.put("output", "");
				requstParam.put("TOrL", "");
				requstParam.put("way", "");
				requstParam.put("carCategoryName", "");
				requstParam.put("carCategoryId", "");
				requstParam.put("competitiveCategoryName","");
				requstParam.put("carmodelid", "");
				requstParam.put("year", "");
				
				
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("dataStructure",aiuiMap.get("page"));
				map.put("scene", 0);
				map.put("skill",0);
				map.put("msgType",0);
				map.put("flag", 0);
				map.put("msg", "返回成功");
				map.put("speak", aiuiMap.get("answer"));
				map.put("url","");
				map.put("set", new Object());
				map.put("skillList",new Object[]{});
				map.put("requestParam",requstParam);
				GsonBuilder gb= new GsonBuilder();
				gb.disableHtmlEscaping();
				String json = gb.create().toJson(map);
				//System.out.println(json);
				//返回参数
				ajaxOutput(response, JSON.toJSONString(map));
				return;
			}
			
		}else{
			try {
				/*String post = HttpUtil.post("http://106.14.136.38:8000/action/lodJson/loadMess?&code="+URLEncoder.encode(code,"utf-8")+"&answer="+URLEncoder.encode(question,"utf-8"), "",  "");
				LogUtil.info("调用机器人云平台返回代码"+post);
				ajaxOutput(response, post);*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public static  void ajaxOutput(HttpServletResponse response,String outputString){
		response.setContentType("text/html; charset=utf-8");
		try{
			//System.out.println("---------------------------" + outputString+ "--------------------------------------------");
			response.getWriter().write(outputString);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
}
